/* Include the header files. */
#include "SDL.h"
#include "SDL_image.h"
#include "string"

/* Screen Attributes. */
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT =  480;
const int SCREEN_BPP = 32;

/* The surface. */
SDL_Surface *pBackground = NULL;
SDL_Surface *pScreen = NULL;
SDL_Surface *pFoo = NULL;

/* Event structure. */
SDL_Event event;

/* Image loader function. */
SDL_Surface *load_image( std::string filename )
{
	/* The initial image holder. */
	SDL_Surface *pPreImage = NULL;

	/* The optimized image holder. */
	SDL_Surface *pOptimizedImage = NULL;

	/* Load the image. */
	pPreImage = IMG_Load( filename.c_str() );

	/* Check if the image is loaded, if yes, then optimize it. */
	if( NULL != pPreImage )
	{
		/* Create the optimized image. */
		pOptimizedImage = SDL_DisplayFormat( pPreImage );

		/* Free the pre-loaded image. */
		SDL_FreeSurface( pPreImage );

		/* Check to see if we have the optimized image. */
		if( NULL != pOptimizedImage )
		{
			/* Map the color key. */
			Uint32 i32ColorKey = SDL_MapRGB( pOptimizedImage->format, 0, 0xFF, 0xFF );

			/* Set all the pixels. */
			SDL_SetColorKey( pOptimizedImage, SDL_SRCCOLORKEY, i32ColorKey );
		}
	}

	/* Return the optimized image. */
	return pOptimizedImage;
}

/* Apply the image function. */
void apply_surface( int x, int y, SDL_Surface *pSource, SDL_Surface *pDestination )
{
	/* Temporary rectangle to hold the offset. */
	SDL_Rect offset;

	/* Get the offsets. */
	offset.x = x;
	offset.y = y;

	/* Blit the surface. */
	SDL_BlitSurface( pSource, NULL, pDestination, &offset );
}

/* File loader (Image loader) function. */
bool load_file( void )
{
	/* Load the background image. */
	pBackground = load_image( "background.png" );

	/* Check to see if we have loaded the image. */
	if( NULL == pBackground )
	{
		return false;
	}

	/* Load the stick figure. */
	pFoo = load_image( "foo.png" );

	/* Check to see if we have loaded the image. */
	if( NULL == pFoo )
	{
		return false;
	}

	/* Else, this means that the images have been loaded fine, so return 1. */
	return true;
}

/* SDL initialize function. */
bool init_SDL( void )
{
	/* Initialize all the SDL subsystem .*/
	if( SDL_Init( SDL_INIT_EVERYTHING ) == 1 )
	{
		/* We were not able to initialize SDL properly, QUIT. */
		return false;
	}

	/* Set up the render screen. */
	pScreen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

	/* Check to see if we have properly initialized the screen. */
	if( NULL == pScreen )
	{
		/* QUIT. */
		return false;
	}

	/* Set the window caption. */
	SDL_WM_SetCaption( "aaah..funny image", NULL );

	/* If everything went as wanted, return true. */
	return true;
}

/* Clean up the assets. */
void clear_SDL( void )
{
	SDL_FreeSurface( pBackground );
	SDL_FreeSurface( pFoo );

	/* Quit SDL. */
	SDL_Quit();
}

/* main Function. */
int main( int argc, char** argv )
{
	/* A quit flag. */
	bool bQuit = false;

	/* Initialize. */
	if( false == init_SDL() )
	{
		/* Error initializing SDL. */
		return 1;
	}

	/* Load all the files. */
	if( false == load_file() )
	{
		/* Error loading the files. */
		return 1;
	}

	/* Apply the surface. */
	apply_surface( 0, 0, pBackground, pScreen );
	apply_surface( 240, 190, pFoo, pScreen );

	/* Update the screen. */
	if( SDL_Flip( pScreen ) == -1 )
	{
		/* QUIT. */
		return 1;
	}

	/* While the user has not quit, UPDATE. */
	if( false == bQuit )
	{
		/* While there is an event to handle. */
		while( SDL_PollEvent( &event ) )
		{
			/* Check if the the user has XED the window. */
			if( SDL_QUIT == event.type )
			{
				/* Quit the window. */
				bQuit = true;
			}
		}
	}

	/* Free all the assets. */
	clear_SDL();
}

